//
//  ExpertListView.m
//  yonghu
//
//  Created by mac on 2018/5/24.
//  Copyright © 2018年 com.DJS. All rights reserved.
//

#import "ExpertListView.h"
#import "selectLayer.h"
#import "ExpertDetailsViewController.h"
#import "ExpertListTableViewCell.h"
@implementation ExpertListView
-(void)creatView{
    _tableArr=[NSMutableArray arrayWithCapacity:0];
    self.backgroundColor=_LightGrey;
    _order=0;
    UIView *topView=[UIView new];
    [self addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(NAV_HEIGHT);
        make.height.mas_equalTo(50);
    }];
    topView.backgroundColor=[UIColor whiteColor];
    
    _view1=[UIView new];
    [topView addSubview:_view1];
    [_view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_mainW/2);
    }];
    
    UITapGestureRecognizer *price = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectPrice:)];
    [_view1 addGestureRecognizer:price];
    
    CGSize size1=[ZQTools getNSStringWeight:[_selectAddDic objectForKey:@"name"] andViewWight:20 andFont:16];
    
    _label1=[UILabel new];
    [_view1 addSubview:_label1];
    [_label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        if (size1.width>_mainW/2*0.7) {
            make.width.mas_equalTo(_mainW/2*0.7);
        }
        else{
            make.width.mas_equalTo(size1.width);
        }
    }];
    
    _label1.text=[_selectAddDic objectForKey:@"name"];
    _label1.textColor=ColorWithRGB(149, 149, 149);
    _label1.font=[UIFont systemFontOfSize:15];
    
    _imageView1=[UIImageView new];
    [_view1 addSubview:_imageView1];
    [_imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_label1.mas_right).mas_equalTo(5);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(7);
    }];
    _imageView1.image=[UIImage imageNamed:@"向下选择"];
    
  
    _view3=[UIView new];
    [topView addSubview:_view3];
    [_view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_view1.mas_right);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_mainW/2);
    }];
    UITapGestureRecognizer *xiaoLiang = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectXiaLiang:)];
    [_view3 addGestureRecognizer:xiaoLiang];
    
    CGSize size3=[ZQTools getNSStringWeight:@"智能排序" andViewWight:20 andFont:16];
    
    _label3=[UILabel new];
    [_view3 addSubview:_label3];
    [_label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        if (size3.width>_mainW/2*0.7) {
            make.width.mas_equalTo(_mainW/2*0.7);
        }
        else{
            make.width.mas_equalTo(size3.width);
        }
    }];
    _label3.text=@"智能排序";
    _label3.textColor=ColorWithRGB(149, 149, 149);
    _label3.font=[UIFont systemFontOfSize:15];
    
    _imageView3=[UIImageView new];
    [_view3 addSubview:_imageView3];
    [_imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_label3.mas_right).mas_equalTo(5);
        make.centerY.mas_equalTo(0);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(7);
    }];
    _imageView3.image=[UIImage imageNamed:@"向下选择"];
    
   
    UIView *xianView=[UIView new];
    [topView addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.width.mas_equalTo(1);
        make.top.bottom.mas_equalTo(0);
    }];
    xianView.backgroundColor=_LightGrey;

    
    _tableView=[BaseTableView new];
    [self addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(topView.mas_bottom);
    }];
    _tableView.tag=2;
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.backgroundColor=_LightGrey;

    _page=1;

    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{


        [_tableView.mj_footer endRefreshing];
        [_tableView.mj_header endRefreshing];
        [_tableArr removeAllObjects];
        _page=1;
        [self loadData];
    }];
    [self loadData];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag==2) {
        return _tableArr.count;
    }
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView.tag==1) {
        UITableViewCell *cell=[[UITableViewCell alloc] init];
        cell.textLabel.text=_tableArr2[indexPath.row];
        cell.textLabel.textAlignment=NSTextAlignmentCenter;
        cell.textLabel.font=[UIFont systemFontOfSize:18];
        cell.textLabel.textColor=ColorWithRGB(49, 49, 49);
        return cell;
    }
    else{
        ExpertListTableViewCell *cell=[[ExpertListTableViewCell alloc] init];
        cell.dic=_tableArr[indexPath.row];
        [cell creatView];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    return nil;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==2) {
        return 130;
    }
    return 60;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [_view removeFromSuperview];
    
    
    
    if (tableView.tag==1) {
        [_tableArr removeAllObjects];
        _order=(int)indexPath.row;
        
        [ZQTools cleanViewSubviews:_view3];
        NSString *Str=_tableArr2[indexPath.row];
        if (indexPath.row==1||indexPath.row==2) {
            Str=@"评价";
        }
        if (indexPath.row==3||indexPath.row==4) {
            Str=@"执业年限";
        }
        CGSize size1=[ZQTools getNSStringWeight:Str andViewWight:20 andFont:16];
        
        _label3=[UILabel new];
        [_view3 addSubview:_label3];
        [_label3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            if (size1.width>_mainW/3*0.7) {
                make.width.mas_equalTo(_mainW/3*0.7);
            }
            else{
                make.width.mas_equalTo(size1.width);
            }
        }];
        _label3.text=Str;
        _label3.textColor=ColorWithRGB(149, 149, 149);
        _label3.font=[UIFont systemFontOfSize:15];
        
        _imageView3=[UIImageView new];
        [_view3 addSubview:_imageView3];
        [_imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_label3.mas_right).mas_equalTo(5);
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(7);
        }];
        _imageView3.image=[UIImage imageNamed:@"向下选择"];
        [_tableArr removeAllObjects];
        _page=1;
        [self loadData];
    }
    if (tableView.tag==2) {
        ExpertDetailsViewController *LSXQ=[[ExpertDetailsViewController alloc] init];
        LSXQ.dic=_tableArr[indexPath.row];
        [ZQTools pushNextViewController:self.viewController andRootController:LSXQ];
    }
    
}


-(void)selectPrice:(UITapGestureRecognizer *)tap{
    
    
    
    selectLayer *lawView=[selectLayer new];
    [self addSubview:lawView];
    [lawView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    lawView.abc=1;
    lawView.shengArr=_shengArr;
    [lawView creatView];
    lawView.customSelectedBlock = ^ (NSDictionary *dic)
    {
        
        _selectAddDic=dic;
        [_label1 removeFromSuperview];
        [_imageView1 removeFromSuperview];
        CGSize size1=[ZQTools getNSStringWeight:[_selectAddDic objectForKey:@"name"] andViewWight:20 andFont:16];
        
        _label1=[UILabel new];
        [_view1 addSubview:_label1];
        [_label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            if (size1.width>_mainW/3*0.7) {
                make.width.mas_equalTo(_mainW/3*0.7);
            }
            else{
                make.width.mas_equalTo(size1.width);
            }
        }];
        _label1.text=[_selectAddDic objectForKey:@"name"];
        _label1.textColor=ColorWithRGB(149, 149, 149);
        _label1.font=[UIFont systemFontOfSize:15];
        
        _imageView1=[UIImageView new];
        [_view1 addSubview:_imageView1];
        [_imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_label1.mas_right).mas_equalTo(5);
            make.centerY.mas_equalTo(0);
            make.width.mas_equalTo(15);
            make.height.mas_equalTo(7);
        }];
        _imageView1.image=[UIImage imageNamed:@"向下选择"];
        
        [_tableArr removeAllObjects];
        _page=1;
        [self loadData];
    };
}

-(void)selectXiaLiang:(UITapGestureRecognizer *)tap{
    
    
    _tableArr2=[NSArray arrayWithObjects:@"评分升序排序",@"评价由低到高",@"评价由高到低", nil];
    _view=[UIView new];
    [self addSubview:_view];
    [_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.width.mas_equalTo(_mainW);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
    }];
    _view.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    UIView *view=[UIView new];
    [_view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64);
        make.width.mas_equalTo(_mainW);
        make.height.mas_equalTo(_mainH*0.4);
        make.left.mas_equalTo(0);
    }];
    view.backgroundColor=[UIColor whiteColor];
    
    BaseTableView *tableView2=[BaseTableView new];
    [view addSubview:tableView2];
    [tableView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    tableView2.tag=1;
    tableView2.delegate=self;
    tableView2.dataSource=self;
    
}

-(void)loadData{

    NSString *city_name=[_selectAddDic objectForKey:@"name"];
    
    
    NSDictionary *userDic = [NSKeyedUnarchiver unarchiveObjectWithFile:_userModelFile];

    NSNumber *waiter_id1=[userDic objectForKey:@"user_id"];
    NSString *user_id=[NSString stringWithFormat:@"%@",waiter_id1];

    NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithCapacity:0];
    [dic setObject:city_name forKey:@"city_name"];
    [dic setObject:@"0" forKey:@"user_role"];
    [dic setObject:user_id forKey:@"user_id"];
    [dic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"page"];
    [dic setObject:@"5" forKey:@"pagesize"];
    [dic setObject:[NSString stringWithFormat:@"%d",_order] forKey:@"order_by"];
    [dic setObject:@"" forKey:@"keywords"];
    [ZQTools AFNPostDataUrl:@"user/waiter/get_expert_list" Dict:dic andTableView:nil andViewController:self.viewController andSuccessBlock:^(id responseObject) {
        
        NSArray *typeArr=responseObject;
        [_tableArr addObjectsFromArray:typeArr];
        [_tableView reloadData];
        
        if (typeArr.count!=0) {
            _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [_tableView.mj_footer endRefreshing];
                [_tableView.mj_header endRefreshing];
                _page=_page+1;
                [self loadData];
            }];
        }
        else{
            _tableView.mj_footer.hidden=YES;
            
        }
        
    } anderrorBlock:nil];
    
}
#pragma mark————————————————————点击屏幕关闭键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self ];
    if (touchPoint.y>64+_mainH*0.4) {
        [_view removeFromSuperview];
    }
}


@end
